home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / VideoToolboxSources / PrintfExit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-29  |  2.0 KB  |  73 lines  |  [TEXT/KAHL]

  1. /* 
  2. PrintfExit.c
  3.  
  4. PrintfExit(const char *format,...);
  5. is normally equivalent to calling printf and exit.
  6.  
  7. Lots of VideoToolbox routines, when they find a really grievous error, print out
  8. a message and exit. Having one routine that does both makes the code slightly
  9. neater, since the if statement then doesn't need braces. Furthermore, both of
  10. these functions, printf, and exit are liable to break in foreign environments,
  11. e.g when running as a MEX resource under MATLAB. Now the problem is confined to
  12. this file, where it can be handled by conditional compilation.
  13.  
  14. Since PrintfExit is called only when we're near death, it seems prudent to make
  15. sure there's enough stack space before calling printf, which crashes if there's
  16. less than about 4500 byts of stack. Note that StackGrow() moves memory.
  17.  
  18. I've replaced all calls to exit() in the entire VideoToolbox by calls to
  19. PrintfExit().
  20.  
  21. StackGrow() is defined in VideoToolbox.h.
  22.  
  23. HISTORY:
  24. 2/20/93    dgp    Wrote it based on conversation with David Brainard.
  25. 7/9/93    dgp    Test MATLAB in #if instead of #ifdef.
  26. 9/12/93    dgp    Moved Required() to Require.c.
  27. 9/15/93    dgp    Added "const" to prototype.
  28. */
  29. #include "VideoToolbox.h"
  30. #include <stdarg.h>            /* for variable-number-of-argument macros */
  31.  
  32. int PrintfExit(const char *format,...)
  33. {
  34.     va_list args;
  35.     int i;
  36.     long qD=0;
  37.   
  38.     #if MAC_C
  39.         /*
  40.         The main program may have changed the current device. Let's
  41.         restore the main device before doing the printf.
  42.         */
  43.         Gestalt(gestaltQuickdrawVersion,&qD);
  44.         if(qD>=gestalt8BitQD)SetGDevice(GetMainDevice());
  45.         
  46.         if(StackSpace()<6000)StackGrow(6000-StackSpace());
  47.     #endif
  48.     #if !MATLAB
  49.         #if MAC_C
  50.             /* printf crashes if there's less than about 4500 bytes of stack space */
  51.             if(StackSpace()<5000){
  52.                 SysBeep(20);
  53.                 exit(EXIT_FAILURE);
  54.             }
  55.         #endif
  56.         va_start(args,format);
  57.         i=vfprintf(stdout,format,args);
  58.         va_end(args);
  59.         exit(EXIT_FAILURE);
  60.     #else
  61.     {
  62.         char s[256];
  63.         
  64.         va_start(args,format);
  65.         i=vsprintf(s,format,args);
  66.         va_end(args);
  67.         mex_error(s);    // Ask MATLAB to report the error.
  68.     }
  69.     #endif
  70.     return 0;            // can't get here
  71. }
  72.  
  73.